MC-10 Operation and Language Reference Manual Chapter 11

11/OPERATORS AND CONTROLLED LOOPS Micro Color BASIC recognizes both "relational operators" and "logical operators". Relational operators allow Micro Color BASIC to compare values, make a decision about those values, then take appropriate action. There are three relational operators that enable the MC-10 to compare one value with another: Is greater than (>). Is less than (<). Is equal to (=). You can combine these to produce three more operators: Is not equal to (<>). Is less than or equal to (<=). Is greater than or equal to (>=). (If you're not familiar with greater than and less than symbols, just remember that the smaller part of the < symbol points to the smaller of the two quantities being compared. For instance, 1<2 and 4>3.) By adding these six relational operators to the four math operators described in Chapter 5 (+, -, *, /), you can create a powerful system of comparing and calculating that becomes the central core of everything that follows. Logical operators, on the other hand, simply "test" values to see if they are true or false (in electrical terms, on or off). You can combine two logical operations (tests) and then test that value. For instance, if the complete operation you're testing is composed of 1<2 AND 2<3 then both elements must be true before the test can pass. Or if it is necessary for only one element to be true for the test to pass, you might say either 1<2 OR 2<3. Relational Operators
IF test THEN action test is the value which is being compared and must include a relational operator (>, <, or =). action is the operation performed once test has been compared.
Even though you may not realize it, you're probably familiar with how IF...THEN statements work. For instance: You need to rake the leaves in the backyard. IF the rake is in the garage, you'll get it and THEN rake the yard. IF it is not in the garage, you'll look in the basement, THEN rake the yard. Try this program to see how the MC-10 uses IF...THEN: 10 INPUT "GUESS THE NUMBER I'M THINKING OF (1 TO 5)";N 20 REM *** THE NUMBER IS 4 *** 30 IF N > 4 THEN GOSUB 100 40 IF N < 4 THEN GOSUB 100 50 IF N = 4 THEN GOTO 200 60 GOTO 10 70 END 100 PRINT "WRONG NUMBER - TRY AGAIN" 110 RETURN 200 PRINT "RIGHT! THE NUMBER IS 4" 210 END Line 30 says, "IF the number you type in is greater than 4, THEN go down to line 100 and print the message that the answer is wrong, then return to try again". Line 40 says, IF the number you type in is less than 4, THEN go down to line 100 and print the message that the answer is wrong, then return to try again". Finally, line 50 says, "IF the number you type in is exactly 4, THEN go to line 200 and print the message that the answer is correct". By combining relational operators, we can save typing (and memory). Change the program by deleting lines 30 and 40 and typing this line: 30 IF N <> 4 THEN GOSUB 100 The program works the same, but you've shortened it by a line. IF...THEN and the Implied GOTO IF...THEN has the additional feature of "implying" the command GOTO. For instance, line 50 can be shortened to this: 50 IF N = 4 THEN 200 This means the same as "IF N=4 THEN GOTO program line 200". Anytime a line number follows THEN, Micro Color BASIC will goto that line. There Has To Be An Easier Way... Both IF and THEN can be entered using the Command Function keys. To enter IF, type: [CONTROL][G] To enter THEN, type: [CONTROL][H] Logical Operators (AND/OR/NOT) Besides the relational operators IF and THEN, Micro Color BASIC also has the "logical operators" AND, OR, and NOT. These perform TRUE/FALSE tests and are used in conjunction with IF...THEN.
IF test1 THEN action1 AND/OR/NOT test2 THEN action2 test1 is the value which is being compared and must include a relational operator (>, <, or =). action1 is the operation performed once test1 has been compared. test2 is the alternate value which is being compared and must include a relational operator (>, <, or =). action2 is the alternate operation performed once test2 has been compared.
AND takes two tests and checks whether both pass or not. If either test fails, then "test AND test" fails; if both tests pass, then "test AND test" passes. OR takes two test and checks whether either test passes or not. If at least one passes, "test OR test" passes; if both tests fail, then "test OR test" fails. NOT takes a single test and checks whether it passes or not. If the test fails, "Not test" passes; if test passes, "NOT test" fails. To understand the concept of logical operators before you begin programming with them, see if you can predict the answers to the following tests: 1. If (1=1) AND (2=3), then PASS (Yes or No). 2. If (1=1) OR (2=3), then PASS (Yes or No). 3. If NOT(1=1 AND 2=3), then PASS (Yes or No). 4. IF NOT(1=1 OR 2=3), then PASS (Yes or No). 5. IF NOT(NOT 1=1 OR 2=3), then PASS (Yes or No). Ready for the answers? 1. No, this test would not pass. 2. Yes, this is true. 3. Yes, this is also true. 4. No, this test would not pass. 5. And Yes, this test would pass. Here's a short program that uses logical operators: 5 INPUT "WHAT IS THE STUDENT'S NAME"; N$ 10 INPUT "WHAT IS THE FINAL GRADE"; F 20 INPUT "WHAT IS THE MIDTERM GRADE"; M 30 INPUT "WHAT IS THE GRADE FOR HOMEWORK"; H 40 IF (F > 60) OR (M > 70) AND (H > 75) THEN 70 50 PRINT "FAILED" 60 END 70 PRINT N$ "PASSED" In this example, the > sign is used instead of the = as a logical test. The student passes if: He or she has a final grade over 60 OR a midterm grade over 70 AND a homework grade over 75. The next program mixes equals, greater than, and less than signs in the same program. It determines and reports whether the two numbers you input are both positive, both negative, or have different signs. 5 REM *** INPUT NUMBERS AND INCLUDE THE SIGNS *** 10 INPUT "FIRST NUMBER"; X 20 INPUT "SECOND NUMBER"; Y 30 IF (X >= 0) AND (Y >= 0) THEN 70 40 IF (X < 0) AND (Y < 0) THEN 90 50 PRINT "OPPOSITE SIGNS" 60 END 70 PRINT "BOTH POSITIVE" 80 END 90 PRINT "BOTH NEGATIVE" 100 END FOR...TO...STEP...NEXT GOTO and GOSUB allow you to create simple program loops in which you can loop back through a program. The problem with simple loops, however is that they are usually "endless"-they go on forever until you press [BREAK] or turn off the Computer. FOR...NEXT allows you to create "controlled" loops which loop only as many times as you want-10 times, 50 times, 100 times, etc.
FOR counter = start value TO end value STEP increment counter is a numeric variable start value is the numeric expression where counting is to begin. end value is the numeric expression where counting is to stop. increment is the number that will be added to counter each time the loop is repeated. STEP increment is optional; if omitted, 1 is used. If increment is a negative number, the loop counts down instead of up.
NEXT counter counter is a numeric variable
A major difference between a computer and a calculator is the computer's ability to do the same thing over and over. FOR...NEXT is such an important part of program repetition that few of the programming areas we'll explore from now on will exclude it. Its simplicity and variations are the heart of its effectiveness. Type in this program: 10 REM *** PRINT MESSAGE FIVE TIMES *** 20 PRINT "THIS IS THE MESSAGE" 30 PRINT "THIS IS THE MESSAGE" 40 PRINT "THIS IS THE MESSAGE" 50 PRINT "THIS IS THE MESSAGE" 60 PRINT "THIS IS THE MESSAGE" 70 END When you RUN it, it displays the appropriate message five times. It works-but it's awkward. FOR...NEXT can do the same thing only more efficiently: 10 REM *** PRINT MESSAGE FIVE TIMES *** 20 FOR X = 1 TO 5 30 PRINT "THIS IS THE MESSAGE" 40 NEXT X 50 END FOR...NEXT does more than just count imaginary numbers, however. You can also use it to increment what appears on the TV Screen. Use the command sequence like this: 10 REM *** PRINTS THE MESSAGE AND THE NUMBER *** 20 FOR N = 1 TO 5 30 PRINT "THIS IS NUMBER"; N 40 NEXT N 50 END What happens in both cases is that line 20 sets the counter at 1; line 30 prints the message; line 40 says to "loop" to the next value of N (which is 2), execute the statement, and continue until the value of N is 5. When N=5, program control drops to the next statement, which is line 50. Notice that in this case, a program statement is executed between the FOR and the NEXT. If you want to have a short delay in program execution, tell the MC-10 to count in between. For example, insert this line: 35 FOR DELAY = 1 TO 300: NEXT DELAY Now when you RUN it, the MC-10 prints the message, counts to 300, prints the next message, counts to 300, etc. As you can see, Micro Color BASIC allow you to have "nested loops" or loops within loops. Another way to create nested loops is to have a FOR...NEXT loop inside a GOTO loop. If you do this, it's essential that the innermost loop be completely contained inside the outermost loop. In other words, when you open one loop inside another, you must close the inner loop before closing the outer loop:
10 FOR J = 1 TO 3 | 10 FOR J = 1 TO 3
20 FOR J2 = 1 TO 2 | 20 FOR J2 = 1 TO 2
30 NEXT J2 | 30 NEXT J
40 NEXT J | 40 NEXT J2
50 END | 50 END
Right! | Wrong!
To see a nested loop in action, RUN the following 24 hour, stop watch program. 10 INPUT "ENTER THE CURRENT HOUR"; HR 20 INPUT "ENTER THE CURRENT MINUTE"; MN 30 INPUT "ENTER THE CURRENT SECOND"; SC 40 FOR HR = HR TO 23 50 FOR MN = MN TO 59 60 FOR SC = SC TO 59 70 PRINT HR; ":"; MN ":"; SC 80 FOR T = 0 TO 735 90 NEXT T 100 NEXT SC: SC = 0 110 NEXT MN: MN = 0 120 NEXT HR: HR = 0 130 GOTO 40 In the above program, the Seconds counter is the innermost loop; the Minutes counter is the middle loop; and the minutes reset to zero in the outer loop. Remember to close inner loops before outer loops. Are you ready for a little advanced programming? In other words, do you want to put some of the things you've learned to work? If so, modify the program above so that it becomes an alarm clock. Experiment on your own, then add the lines below to see one way to do it: 5 INPUT "WHAT HOUR DO YOU WANT TO GET UP":HA 8 INPUT "WHAT MINUTE DO YOU WANT TO GET UP";MA 95 IF HR = HA THEN GOSUB 200 200 IF MN = MA THEN GOSUB 300 210 RETURN 300 PRINT "TIME TO GET UP!" 310 SOUND 100,100 320 END The STEP Option Normally, FOR...NEXT "steps" through the counter 1 count at a time. However, if you want to increment at a different rate, use the STEP option and specify the counting increment. For instance: 10 FOR X = 1 TO 100 STEP 5 20 FOR DELAY = 1 TO 300: NEXT DELAY 30 PRINT X 40 NEXT X 50 END When you RUN the program, it won't display every number between 1 and 100. Instead, it will display every 5th number (1, 6, 11, 16, 21, 26, etc.). There Has To Be An Easier Way... Notice that you can use the Command Function keys to make entering FOR, NEXT, and STEP a little easier. To enter FOR, type: [CONTROL][U] To enter NEXT, type: [CONTROL][I] To enter STEP, type: [CONTROL][O]

Comments should be directed to the following email address:
dsbrain@neosplice.com